<template>
  <div class="similar-question-dialog" ref="similarQuestionDialog">
    <Modal
      v-if="props.visible"
      title="相似题"
      @closeModal="closeDialog"
      @cancel="closeDialog"
    >
      <template #modalContent>
        <div class="modal-content" v-loading="loading">
          <template v-if="list && list.length > 0">
            <div v-for="(item, index) in list" :key="index" class="list">
              <similar-question-item
                :content="item.content"
                :isShowAnswer="item.isShowAnswer"
                :index="index + 1"
                :isChecked="item.isChecked"
                @checked="selectQuestion($event, item.content, index)"
                @show-answer="showAnswer($event, index)"
                @show-video="showVideoModal"
              ></similar-question-item>
            </div>
          </template>
          <no-data v-else></no-data>
        </div>

        <div class="confirm">
          <button
            class="btn-primary"
            @click="showAssessment"
            :class="{ disabled: selectedQuestion.length < 1 }"
          >
            查看讲评<span v-if="selectedQuestion.length > 0"
              >({{ selectedQuestion.length }})</span
            >
          </button>
        </div>
      </template>
    </Modal>
  </div>
  <div class="video-dialog">
    <Modal
      v-if="showVideo"
      @closeModal="showVideo = !showVideo"
      @cancel="showVideo = !showVideo"
    >
      <template #modalContent>
        <div class="video-content">
          <header class="video-content__header">
            <label class="close-icon" @click="showVideo = !showVideo"></label>
          </header>
          <VideoPlayer :url="video.url" :poster="video.poster"></VideoPlayer>
        </div>
      </template>
    </Modal>
  </div>
</template>
<script setup>
import SimilarQuestionItem from './SimilarQuestionItem.vue'
import VideoPlayer from './VideoPlayer.vue'
import Modal from './Modal.vue'
import NoData from './NoData.vue'
import { useAuthStore } from '@/stores/auth-store'
import { useComment } from '@/stores/comment'
import { getXkwSimilarQuestions } from '@/apis/target-boost/index'
import { ref, reactive, watch } from 'vue'
import { ElMessage } from 'element-plus'

const commentStore = useComment()
const userStore = useAuthStore()
const similarQuestionDialog = ref(null)
const props = defineProps({
  visible: {
    default: false,
    type: Boolean
  }
})
const emits = defineEmits(['close-dialog'])
const showVideo = ref(false)
const list = ref([])
const video = reactive({
  url: '',
  poster: ''
})
const activeKey = ref(1)
const selectedQuestion = ref([])
const loading = ref(false)

const changeTab = val => {
  activeKey.value = val
}

const getList = async () => {
  loading.value = true
  const { data } = await getXkwSimilarQuestions(
    commentStore.currentQuestion.question_id,
    {
      content: commentStore.currentQuestion.question_detail.content
    }
  )
  list.value = (data?.questions || []).map(item => {
    return {
      isShowAnswer: false,
      isChecked: false,
      content: item
    }
  })
  loading.value = false
}

const selectQuestion = (e, item, i) => {
  const temp = list.value
  temp[i].isChecked = e.isChecked
  const current = item.id
  const index = selectedQuestion.value.findIndex(item1 => item1.id === current)
  if (index > -1) {
    selectedQuestion.value.splice(index, 1)
  } else {
    selectedQuestion.value.push({
      questionImg: e.questionImg,
      ...item
    })
  }
}

const closeDialog = () => {
  selectedQuestion.value = []
  commentStore.setSimilar({
    isShow: false,
    list: []
  })
  emits('close-dialog')
}

const showAnswer = (e, index) => {
  const temp = list.value
  temp[index].isShowAnswer = e
}

const showVideoModal = val => {
  showVideo.value = !showVideo.value
  video.url = val.url
  video.poster = val.poster
}

watch(
  () => props.visible,
  val => {
    if (val) getList()
  }
)

watch(
  () => commentStore.similar.selectedList,
  val => {
    if (val && val.length === 0) {
      selectedQuestion.value = []
    }
  }
)

const showAssessment = () => {
  if (selectedQuestion.value.length < 1) {
    ElMessage.warning('至少选择一道题目！')
    return
  }
  commentStore.setSimilar({
    isShow: true,
    list: selectedQuestion.value
  })
  emits('close-dialog')
}
</script>
<style lang="less" scoped></style>
<style lang="less" scoped>
.similar-question-dialog {
  :deep(.mask) {
    position: fixed !important;
  }
  :deep(.modal-btn) {
    display: none !important;
  }

  .btn-primary {
    border: 0;
    padding: 0 16px;
    min-width: 80px;
    height: 40px;
    border-radius: 8px;
    background-color: @theme-color;
    font-size: @base-size;
    color: #fff;
    cursor: pointer;

    &:not(:disabled):hover {
      background-color: @theme-color--hover;
    }

    &.disabled,
    &:disabled {
      opacity: 0.4;
      cursor: not-allowed;
    }
  }

  .modal-content {
    height: 650px;
    width: 1200px;
    overflow-y: scroll;
    border-radius: 16px;
    display: flex;
    flex-direction: column;

    &::-webkit-scrollbar {
      display: none;
    }

    .confirm {
      margin: 24px 0 0 24px;
      text-align: right;
      height: fit-content;

      .btn-primary {
        background: rgba(51, 202, 164, 1);
      }
    }

    :deep(.ant-tabs) {
      flex: 1;
      display: flex;
      flex-direction: column;

      .ant-tabs-nav {
        height: fit-content;
      }
      .ant-tabs-content-holder {
        position: relative;
        flex: 1;
        overflow-y: auto;
      }

      .ant-tabs-tab {
        &:hover {
          color: rgba(51, 202, 164, 1);
        }
      }
      .ant-tabs-tab-active {
        * {
          color: rgba(51, 202, 164, 1);
        }
      }

      .ant-tabs-ink-bar {
        background-color: rgba(51, 202, 164, 1);
      }
    }

    .loading {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: rgba(255, 255, 255, 0.75);
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
      z-index: 2;

      :deep(.ant-spin-text) {
        font-size: 18px;
        margin-top: 12px;
        color: rgba(51, 202, 164, 1);
      }

      :deep(.ant-spin-dot) {
        font-size: 40px;
      }

      :deep(.ant-spin-dot-item) {
        background-color: rgba(51, 202, 164, 1);
        webkit-transform: scale(1);
        -ms-transform: scale(1);
        transform: scale(1);
      }

      :deep(.ant-spin-dot i) {
        width: 14px;
        height: 14px;
      }
    }
  }
}

.video-dialog {
  :deep(.mask) {
    background: transparent;
    z-index: 9999;
    .video-content {
      height: 840px;
      width: 1248px;
      border-radius: 24px;
      // background: #000;
      overflow: hidden;
      position: relative;
      display: flex;
      justify-content: center;

      &__header {
        position: absolute;
        top: 0;
        left: 0;
        height: 50px;
        width: 100%;
        border-radius: 24px 24px 0 0;
        z-index: 9999;
        background: rgba(0, 0, 0, 0.3);
        display: flex;
        align-items: center;
        justify-content: flex-end;

        .close-icon {
          cursor: pointer;
          display: inline-block;
          height: 24px;
          width: 24px;
          margin-right: 24px;
          background: url('@/assets/images/comment/close-modal.png') no-repeat
            center/cover;
          &:hover {
            opacity: 0.7;
          }
        }
      }
    }
    .modal-btn,
    .modal-top {
      display: none;
    }

    .modal {
      padding: 0;
    }
  }
}
</style>
